/*********************
* Author:Dzlua
* QQ:505544956
* time:2016/12/18
*/
#include "LuaAnimation.h"
#include "../edui.h"

#ifdef EDUI_USE_LUAPLUS

EDUI_NAMESPACE_BEGIN

#ifdef EDUI_TEST_NEW_DELETE
int LuaAnimation::m_newdeleteNum = 0;
int LuaAnimation::_GetNewDeleteNum()
{
    return m_newdeleteNum;
}
#endif

void* LuaAnimation::GetInterface(void* data)
{
    if (!data) return 0;
    LuaAnimation* animation = new LuaAnimation((Animation*)data);
#ifdef EDUI_TEST_NEW_DELETE
    ++m_newdeleteNum;
    Log("[LuaAnimation] new LuaAnimation : %d, %d", animation, data);
#endif
    return animation;
}

void LuaAnimation::Release()
{
#ifdef EDUI_TEST_NEW_DELETE
    --m_newdeleteNum;
    Log("[LuaAnimation] delete LuaAnimation : %d, %d", this, m_cobj);
#endif
    delete this;
}

void* LuaAnimation::GetCObj()
{
    return m_cobj;
}

LuaAnimation::LuaAnimation(Animation* animation)
{
    m_cobj = animation;
}

LuaAnimation::~LuaAnimation()
{}

//----------------------------------------//

 size_t LuaAnimation::FrameCount()
 {
     return m_cobj->FrameCount();
 }

int LuaAnimation::Start(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsInteger()) m_cobj->Start(args[2].GetInteger());
    else m_cobj->Start();
    return 0;
}

void LuaAnimation::Stop()
{
    m_cobj->Stop();
}

bool LuaAnimation::IsRun()
{
    return m_cobj->IsRun();
}

void LuaAnimation::Reset()
{
    m_cobj->Reset();
}

//----------------------------------------//

void LuaAnimation::Register(LuaState *L)
{
    LuaObject obj = LuaHelperNS::AddToMetas(L, "Animation");
    obj.SetObject("__index", obj);
    obj.RegisterDirect("__NewCObj__", &LuaAnimation::GetInterface);
    
    obj.RegisterObjectDirect("FrameCount", (LuaAnimation*)0, &LuaAnimation::FrameCount);
    obj.RegisterObjectFunctor("Start", &LuaAnimation::Start);
    obj.RegisterObjectDirect("Stop", (LuaAnimation*)0, &LuaAnimation::Stop);
    obj.RegisterObjectDirect("IsRun", (LuaAnimation*)0, &LuaAnimation::IsRun);
    obj.RegisterObjectDirect("Reset", (LuaAnimation*)0, &LuaAnimation::Reset);
}

EDUI_NAMESPACE_END

#endif